using UnityEngine;
using QFramework;
using QAssetBundle;

// 1.请在菜单 编辑器扩展/Namespace Settings 里设置命名空间
// 2.命名空间更改后，生成代码之后，需要把逻辑代码文件（非 Designer）的命名空间手动更改
namespace QFramework.Example
{
    public partial class Enemy : ViewController, IEnemy
    {
        [Header("怪物移动速度")]
        [SerializeField]
        float float_EnemyMoveSpeed = 2;

        [Header("怪物生命值")]
        [SerializeField]
        float float_Hp = 3;

        void Start()
        {
            Global.EnemyCount.Value++;
            // Code Here
        }

        private void FixedUpdate()
        {
            if (Player.Default)
            {
                Vector2 direction = (Player.Default.transform.position - transform.position).normalized;

                SelfRigidbody2D.velocity = direction * float_EnemyMoveSpeed;
            }
        }

        bool bool_Hurt = false;
        public void OnSetHp(float hurt)
        {
            float_Hp += hurt;
            if (!bool_Hurt)
            {
                var floattingText = FindObjectOfType<FloattingTextController>();
                floattingText.OnPlay(transform.position + transform.up * 0.5f, Mathf.Abs(hurt).ToString());
                AudioKit.PlaySound(Sfx.HIT);
                this.Sprite.color = Color.red;
                bool_Hurt = true;
                //延迟函数 
                ActionKit.Delay(0.3f, () =>
                {
                    if (this != null)
                        this.Sprite.color = Color.white;

                    bool_Hurt = false;
                }).Start(this);//全局应用
            }
            if (float_Hp <= 0)
            {
                OnDieEnmey();
            }
        }

        public void OnDieEnmey()
        {
            AudioKit.PlaySound(Sfx.ENEMYDIE);
            Global.OnDropObject(gameObject);
            gameObject.DestroySelfGracefully();
        }

        /// <summary>
        /// 获取当前怪物血量
        /// </summary>
        /// <returns></returns>
        public float OnGetCurrentEnemyHp()
        {
            return float_Hp;
        }

        private void OnDestroy()
        {
            Global.EnemyCount.Value--;
        }

        public void OnSetHpScale(float hpScale)
        {
            float_Hp *= hpScale;
        }

        public void OnSetSpeedScale(float speedScale)
        {
            float_EnemyMoveSpeed *= speedScale;
        }
    }
}
